1 // Fig. 21.4: fig21_04.cpp 2 // Demonstrating reinterpret_cast operator. 3 #include 4 5 int main() 6 { 7 unsigned x = 22, *unsignedPtr; 8 void *voidPtr = &x; 9 char *charPtr = "C++"; 10 11 // cast from void * to unsigned * 12 unsignedPtr = reinterpret_cast< unsigned * >( voidPtr ); 13 14 cout << "*unsignedPtr is " << *unsignedPtr 15 << "\ncharPtr is " << charPtr; 16 17 // use reinterpret_cast to cast a char * pointer to unsigned 18 cout << "\nchar * to unsigned results in: " 19 << ( x = reinterpret_cast< unsigned >( charPtr ) ); 20 21 // cast unsigned back to char * 22 cout << "\nunsigned to char * results in: " 23 << reinterpret_cast< char * >( x ) << endl; 24 25 return 0; 26 } }